//
//  AccountInfoViewController.m
//  ForYourBeauty
//
//  Created by Kelly xu on 2019/6/2.
//  Copyright © 2019 changrenTech. All rights reserved.
//

#import "AccountInfoViewController.h"
#import "TitleContentAccessCell.h"
#import "TitleIconAccessCell.h"
#import "LoginViewController.h"
#import "ImagePickerManager.h"
#import "UploadViewModel.h"
#import "EditOneLineViewController.h"
#import "EditSignatureViewController.h"
#import "EditBindMobileViewController.h"
#import "UserCenterViewModel.h"
@interface AccountInfoViewController ()<UITableViewDelegate,UITableViewDataSource>{
    
}
@property(nonatomic,weak)IBOutlet UITableView *mainTableView;
@property(nonatomic,strong)NSMutableArray *titleArray;
@property(nonatomic,strong)UploadViewModel *uploadViewModel;
@property(nonatomic,strong)NSString *userIconUrl;
@property(nonatomic,strong)NSString *nickName;
@property(nonatomic,strong)NSString *userSignsure;
@property(nonatomic,strong)NSString *userMobile;
@property(nonatomic,strong)UserCenterViewModel *userCenterViewModel;
@end

@implementation AccountInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"设置";
    [self configTableView];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    [self.userCenterViewModel fetchUserInfo:[LoginTools sharedTools].userId cmp:^(UserInfoModel * _Nonnull userModel) {
        [LoginTools sharedTools].userInfoModel = userModel;
        self.userIconUrl = [LoginTools sharedTools].userInfoModel.userIcon;
        self.nickName = [LoginTools sharedTools].userInfoModel.userNickname;
        self.userSignsure = [LoginTools sharedTools].userInfoModel.userSignature;
        self.userMobile = [LoginTools sharedTools].userInfoModel.userMobile;
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.mainTableView reloadData];
        });
    }];
}
-(void)configTableView{
    self.mainTableView.delegate = self;
    self.mainTableView.dataSource = self;
    self.mainTableView.tableFooterView = [UIView new];
    [self.mainTableView registerNib:[UINib nibWithNibName:@"TitleContentAccessCell" bundle:nil] forCellReuseIdentifier:@"TitleContentAccessCell"];
    [self.mainTableView registerNib:[UINib nibWithNibName:@"TitleIconAccessCell" bundle:nil] forCellReuseIdentifier:@"TitleIconAccessCell"];
    self.mainTableView.showsVerticalScrollIndicator = NO;
    self.mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.mainTableView.backgroundColor = [UIColor clearColor];
}
#pragma mark - UITableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.titleArray count]+1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row==0) {
        TitleIconAccessCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TitleIconAccessCell" forIndexPath:indexPath];
        cell.backgroundColor = [UIColor clearColor];
        cell.contentView.backgroundColor = [UIColor clearColor];
        [cell.bgImageView setImage:[[UIImage imageNamed:@"topBg"] stretchableImageWithLeftCapWidth:[UIImage imageNamed:@"topBg"].size.width/2 topCapHeight:[UIImage imageNamed:@"topBg"].size.height/2]];
        [cell.userIcon sd_setImageWithURL:[NSURL URLWithString:self.userIconUrl]];
        return cell;
    }else{
        TitleContentAccessCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TitleContentAccessCell" forIndexPath:indexPath];
        cell.backgroundColor = [UIColor clearColor];
        cell.contentView.backgroundColor = [UIColor clearColor];
        if (indexPath.row==0) {
            [cell.bgImageView setImage:[[UIImage imageNamed:@"topBg"] stretchableImageWithLeftCapWidth:[UIImage imageNamed:@"topBg"].size.width/2 topCapHeight:[UIImage imageNamed:@"topBg"].size.height/2]];
        }else if (indexPath.row<[self.titleArray count]){
            [cell.bgImageView setImage:[[UIImage imageNamed:@"middleBg"] stretchableImageWithLeftCapWidth:[UIImage imageNamed:@"middleBg"].size.width/2 topCapHeight:[UIImage imageNamed:@"middleBg"].size.height/2]];
        }else{
            [cell.bgImageView setImage:[[UIImage imageNamed:@"bottomBg"] stretchableImageWithLeftCapWidth:[UIImage imageNamed:@"bottomBg"].size.width/2 topCapHeight:[UIImage imageNamed:@"bottomBg"].size.height/2]];
        }
        cell.titleLab.text = [self.titleArray objectAtIndex:indexPath.row-1];
        switch (indexPath.row) {
            case 1:
            {
                if (!EC_ISNullStr(self.nickName)) {
                    cell.contentText.text = [NSString stringWithFormat:@"%@",self.nickName];
                }else{
                    cell.contentText.text = @"";
                }
            }
                break;
            case 2:
            {
                if (!EC_ISNullStr(self.userSignsure)) {
                    cell.contentText.text = [NSString stringWithFormat:@"%@",self.userSignsure];
                }else{
                    cell.contentText.text = @"";
                }
            }
                break;
            case 3:
            {
                if (!EC_ISNullStr(self.userMobile)) {
                    cell.contentText.text = [NSString stringWithFormat:@"%@",self.userMobile];
                }else{
                    cell.contentText.text = @"";
                }
            }
                break;
            default:
                break;
        }
        return cell;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 0:
        {
            [[ImagePickerManager sharedImgPicker]showImgActionWithType:ImagePickerTypeAll enableEditing:NO Completion:^(UIImage *usericon, UIImagePickerController *imgPicker) {
                [self.uploadViewModel uploadImg:usericon cmp:^(NSString * _Nonnull userIconUrl) {
                    self.userIconUrl = userIconUrl;
                    [[HttpClient shareClient] postWithUrl:UPDATE_USERINFO params:@{@"id":[LoginTools sharedTools].userId,@"userIcon":self.userIconUrl} suc:^(id results) {
                        dispatch_async(dispatch_get_main_queue(), ^{
                            [self.mainTableView reloadData];
                        });
                    } fail:^(NSString *error, NSInteger status) {
                        Toast(error);
                    }];
                }];
                [imgPicker dismissViewControllerAnimated:YES completion:nil];
            }];
        }
            break;
        case 1:
        {
            EditOneLineViewController *editNickName = [[EditOneLineViewController alloc]init];
            [self.rt_navigationController pushViewController:editNickName animated:YES complete:nil];
        }
            break;
        case 2:
        {
            EditSignatureViewController *editSignature = [[EditSignatureViewController alloc]init];
            [self.rt_navigationController pushViewController:editSignature animated:YES complete:nil];
        }
            break;
        case 3:
        {
            EditBindMobileViewController *editMobile = [[EditBindMobileViewController alloc]init];
            [self.rt_navigationController pushViewController:editMobile animated:YES complete:nil];
        }
            break;
        default:
            break;
    }
}
#pragma mark - instance
-(NSMutableArray*)titleArray{
    if (!_titleArray) {
        _titleArray = [[NSMutableArray alloc]init];
        [_titleArray addObjectsFromArray:@[@"昵称",@"个性签名",@"手机"]];
    }
    return _titleArray;
}
-(UserCenterViewModel *)userCenterViewModel{
    if (!_userCenterViewModel) {
        _userCenterViewModel = [[UserCenterViewModel alloc]init];
    }
    return _userCenterViewModel;
}
#pragma mark - op
-(void)logOut{
    LoginViewController *login = [[LoginViewController alloc]init];
    RTRootNavigationController *nav = [[RTRootNavigationController alloc]initWithRootViewControllerNoWrapping:login];
    [JumpTools setRootCtrl:nav];

}
-(UploadViewModel*)uploadViewModel{
    if (!_uploadViewModel) {
        _uploadViewModel = [[UploadViewModel alloc]init];
    }
    return _uploadViewModel;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
